package moonlit.chill.pay.service.impl.ali;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import lombok.extern.slf4j.Slf4j;
import moonlit.chill.pay.cache.PayConfigDataCache;
import moonlit.chill.pay.constants.Constants;
import moonlit.chill.pay.service.PayStrategy;
import moonlit.chill.pay.util.AliPayClientUtil;
import moonlit.chill.pay.vo.PayConfig;
import moonlit.chill.pay.vo.TradeParam;
import moonlit.chill.pay.vo.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author MoonlitChill
 * @date 2024/4/15 16:10
 */
@Slf4j
@Service("aliCodePayStrategy")
public class AliCodePayStrategyServiceImpl implements PayStrategy {
    @Resource
    private PayConfigDataCache payConfigDataCache;

    @Autowired
    private AliPayClientUtil aliPayClientUtil;

    @Override
    public PayResult<?> payMethod(TradeParam tradeParam) {
        log.info(String.format("支付宝付款码支付参数:%s", JSONUtil.toJsonStr(tradeParam)));
        PayConfig payConfig = payConfigDataCache.getPayConfig();
        PayResult<String> result = new PayResult<>();
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        AlipayTradePayModel model = new AlipayTradePayModel();
        String outTradeNo = tradeParam.getTransNum();
        result.setTransNum(outTradeNo);
        tradeParam.setTransNum(outTradeNo);
        model.setOutTradeNo(outTradeNo);
        model.setTotalAmount(tradeParam.getTotalFee().toString());
        model.setScene("bar_code");
        model.setSubject(tradeParam.getPayTitle());
        model.setBody(JSONUtil.toJsonStr(tradeParam.getAdditionalInfo()));
        model.setAuthCode(tradeParam.getAuthCode());
        model.setTimeoutExpress("1m");
        request.setBizModel(model);
        request.setNotifyUrl(payConfig.getNotifyUrl());
        try {
            AlipayClient aliPayClient = aliPayClientUtil.getAliPayClient(payConfig);
            AlipayTradePayResponse payResponse;
            if (payConfig.getAliType() == 0){
                //证书模式
                payResponse = aliPayClient.certificateExecute(request, "GET");
            } else {
                //密钥模式
                payResponse = aliPayClient.execute(request, "GET");
            }
            log.info(String.format("支付宝付款码支付参数:%s  返回数据:%s", JSONUtil.toJsonStr(tradeParam), JSONUtil.toJsonStr(payResponse)));
            if (payResponse.isSuccess()) {
                if ("10000".equals(payResponse.getCode())) {
                    //10000 代表成功
                    result.setSuccess(Boolean.TRUE);
                    result.setResult(JSONUtil.toJsonStr(payResponse));
                } else if ("10003".equals(payResponse.getCode()) || "20000".equals(payResponse.getCode())) {
                    result.error(payResponse.getSubMsg(), Constants.PAY_USER_PAYING);
                } else {
                    result.error(payResponse.getSubMsg(), Constants.PAY_FAIL_CODE);
                }
            } else {
                //支付失败
                result.error(payResponse.getSubMsg(), Constants.PAY_FAIL_CODE);
            }
        } catch (Exception e) {
            result.errorWithException(e.getMessage(), Constants.PAY_ERROR_CODE, e);
        }
        return result;
    }

}
